package com.clkj.xiuzheng.controller.shangcheng;

import com.clkj.xiuzheng.pojo.common.Result;
import com.clkj.xiuzheng.pojo.common.StatusCode;
import com.clkj.xiuzheng.pojo.shangcheng.*;
import com.clkj.xiuzheng.service.shangcheng.ScOrderService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/ScOrder")
@CrossOrigin
@Api(value = "/ScOrder", tags = {"Z商城订单管理"})
public class ScOrderController {

    @Autowired
    private ScOrderService scOrderService;

    @ApiOperation(value = "根据(公司id<可选>和订单号<可选>和买家账号<可选>)获取订单列表")
    @PostMapping("/getOrderlist")
    public Result getOrderlist(@ApiParam(name = "公司Id", value = "companyId")@RequestParam(required = false) Integer companyId,
                               @ApiParam(name = "快递单号", value = "orderNumber")@RequestParam(required = false) String orderNumber,
                               @ApiParam(name = "买家账号", value = "account")@RequestParam(required = false) String account,
                               @ApiParam(name = "厂商id", value = "factoryId")@RequestParam(required = false) String factoryId,
                               @RequestParam Integer pageSize,
                               @RequestParam Integer pageNum) {

        try {
            PageInfo list  = scOrderService.getOrderlist(companyId, orderNumber, account,factoryId, pageSize, pageNum);
            return new Result(true, StatusCode.OK,"添加成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }
    @ApiOperation(value = "根据(订单id)获取订单详情")
    @PostMapping("/getOrderInfoById")
    public Result getOrderInfoById(@ApiParam(name = "订单号", value = "orderNumber")@RequestParam String orderNumber) {
        try {
            List<OrderInfo> list  = scOrderService.getOrderInfoById(orderNumber);
            return new Result(true, StatusCode.OK,"添加成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }
    @ApiOperation(value = "根据订单编号发货")
    @PostMapping("/ship")
    public Result ship(@ApiParam(name = "订单编号", value = "id")@RequestParam Integer id,@ApiParam(name = "发货公司", value = "shipMethod")@RequestParam String shipMethod,@ApiParam(name = "快递单号", value = "sendNumber")@RequestParam String sendNumber,@ApiParam(name = "发货人id", value = "sendUserId")@RequestParam Integer sendUserId,@ApiParam(name = "物流公司名称", value = "sendCompanyName")@RequestParam String sendCompanyName) {
        try {
            scOrderService.ship(id,shipMethod,sendNumber,sendUserId,sendCompanyName);
            return new Result(true, StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }
    @ApiOperation(value = "小程序获取我的收货地址")
    @PostMapping("/listAddress")
    public Result listAddress(Integer userId) {
        try {
            List<Address> addressList = scOrderService.listAddress(userId);
            return new Result(true, StatusCode.OK,"查询成功",addressList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation(value = "小程序添加订单")
    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody Order order) {
        try {
             Map map = scOrderService.addOrder(order);
            return new Result(true, StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation(value = "小程序获取我的订单")
    @PostMapping("/listOrders")
    public Result listOrders(Integer status,Integer page,Integer size,Integer userId) {
        try {
            PageInfo map  =scOrderService.listOrders(status,page,size,userId);
            return new Result(true, StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation(value = "小程序取消订单、确认收货")
    @PostMapping("/cancelOrder")
    public Result cancelOrder(Integer orderId,Integer status) {
        try {
            scOrderService.cancelOrder(orderId,status);
            return new Result(true, StatusCode.OK,"查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation(value = "小程序查看物流")
    @PostMapping("/checkWuLiu")
    public Result checkWuLiu(String orderNum) {
        try {
            Map map = scOrderService.checkWuLiu(orderNum);
            return new Result(true, StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation(value = "小程序根据订单号获取订单信息")
    @PostMapping("/listOrderByNumber")
    public Result listOrderByNumber(String orderNum) {
        try {
            Order order = scOrderService.listOrderByNumber(orderNum);
            return new Result(true, StatusCode.OK,"查询成功",order);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation(value = "根据订单id用,隔开字符串 导出订单excel")
    @GetMapping("/exportOrder")
    public Result exportOrder(String Ids, HttpServletResponse response) {
        try {
            scOrderService.exportOrder(Ids,response);
            return new Result(true, StatusCode.OK,"导出成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"导出失败");
        }
    }
    @ApiOperation(value = "导入订单excel")
    @PostMapping("/importOrder")
    public Result importOrder(Integer operatorId,HttpServletRequest request) {
        try {
           Result result = scOrderService.importOrder(operatorId,request);
            return result ;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"导入失败");
        }
    }

    @ApiOperation(value = "小程序订单商品评价")
    @PostMapping("/rateProduct")
    public Result rateProduct(Integer productId,String orderNum ) {
        try {
            scOrderService.rateProduct(productId,orderNum);
            return new Result(true, StatusCode.OK,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作成功");
        }
    }
    @ApiOperation(value = "小程序修改订单信息")
    @PostMapping("/updateOrder")
    public Result updateOrder(@RequestBody Order order) {
        try {
            Map map = scOrderService.updateOrder(order);
            return new Result(true, StatusCode.OK,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作成功");
        }
    }
    @ApiOperation(value = "小程序退货")
    @PostMapping("/returnOrder")
    public Result returnOrder(OrderReturn returnOrder) {
        try {
            scOrderService.returnOrder(returnOrder);
            return new Result(true, StatusCode.OK,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作成功");
        }
    }

    @ApiOperation(value = "小程序获取我的售后记录")
    @PostMapping("/listReturnOrder")
    public Result listReturnOrder(Integer userId) {
        try {
            List<OrderReturn> orderReturnList = scOrderService.listReturnOrder(userId);
            return new Result(true, StatusCode.OK,"操作成功",orderReturnList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作成功");
        }
    }

    @ApiOperation(value = "后台售后,1,确认可退货，2 完成确定---用户退货已收到货进行积分/余额的返回")
    @PostMapping("/sureOrderReturn")
    public Result sureOrderReturn(Integer returnId,Integer infoId,Integer userId,
                                  @ApiParam(name = "flag", value = "0为确认可退货，1为收到退货，返余额和积分")@RequestParam("flag") Integer flag,
                                  @ApiParam(name = "status", value = "1为可退货，3为审核驳回 2为收到退货，返积分和余额")@RequestParam("status") Integer status,
                                  @ApiParam(name = "note", value = "如果为申请驳回，原因")@RequestParam("note") String note,
                                  @ApiParam(name = "returnNumber", value = "申请售后的订单号")@RequestParam("returnNumber") String returnNumber) {
        try {
            Map map = scOrderService.sureOrderReturn(returnId,infoId,userId,flag,status,note,returnNumber);
            return new Result(true, StatusCode.OK,"操作成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作成功");
        }
    }
    @ApiOperation(value = "后台获取售后记录")
    @PostMapping("/listAllReturnOrder")
    public Result listAllReturnOrder() {
        try {
            List<OrderReturn> orderReturnList = scOrderService.listAllReturnOrder();
            return new Result(true, StatusCode.OK,"操作成功",orderReturnList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作成功");
        }
    }



}
